/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package ly.count.ohos.demo;

import ly.count.ohos.sdk.Countly;
import ly.count.ohos.sdk.CountlyConfig;
import ly.count.ohos.sdk.DeviceId;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ScrollView;
import ohos.global.configuration.Configuration;

/**
 * 其他示例页面
 *
 * @since 2021-04-13
 */
public class AbilityExampleOthers extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_example_others);
        findComponentById(ResourceTable.Id_button).setClickedListener(this::onClickViewOther05);
        findComponentById(ResourceTable.Id_button45).setClickedListener(this::onClickViewOther06);
        findComponentById(ResourceTable.Id_button53).setClickedListener(this::onClickViewOther08);
        findComponentById(ResourceTable.Id_button60).setClickedListener(this::onClickViewOther10);
        findComponentById(ResourceTable.Id_button64).setClickedListener(this::onClickTestcrashFilterSample);
        findComponentById(ResourceTable.Id_button65).setClickedListener(this::onClickRemoveAllConsent);
        findComponentById(ResourceTable.Id_button66).setClickedListener(this::onClickGiveAllConsent);
        findComponentById(ResourceTable.Id_button41).setClickedListener(this::onClickHaltAndInit);
        Countly.sharedInstance().onStart(this);

        ScrollView scrollView = (ScrollView) findComponentById(ResourceTable.Id_other_scroll_view);
        scrollView.enableScrollBar(ScrollView.AXIS_Y,true);
    }

    private void onClickViewOther05(Component v) {
        // set user location
        String countryCode = "us";
        String city = "Höuston";
        String latitude = "29.634933";
        String longitude = "-95.220255";
        String ipAddress = null;

        Countly.sharedInstance().setLocation(countryCode, city, latitude + "," + longitude, ipAddress);
    }

    private void onClickViewOther06(Component v) {
        // disable location
        Countly.sharedInstance().disableLocation();
    }

    private void onClickViewOther08(Component v) {
        // Clearing request queue
        Countly.sharedInstance().flushRequestQueues();
    }

    private void onClickViewOther10(Component v) {
        // Doing internally stored requests
        Countly.sharedInstance().doStoredRequests();
    }

    private void onClickTestcrashFilterSample(Component v) {
        Countly.sharedInstance().crashes().recordUnhandledException(new Throwable("A really secret exception"));
    }

    private void onClickRemoveAllConsent(Component v) {
        Countly.sharedInstance().consent().removeConsentAll();
    }

    private void onClickGiveAllConsent(Component v) {
        Countly.sharedInstance().consent().giveConsentAll();
    }

    private void onClickHaltAndInit(Component v) {
        Countly.sharedInstance().halt();

        final String countlyServerUrl = "YOUR_SERVER";
        final String countlyAppKey = "YOUR_APP_KEY";

        CountlyConfig config = (new CountlyConfig(this, countlyAppKey, countlyServerUrl))
            .setIdMode(DeviceId.Type.OPEN_UDID)
            .enableCrashReporting()
            .setLoggingEnabled(true)
            .enableCrashReporting()
            .setViewTracking(true)
            .setAutoTrackingUseShortName(true)
            .setRequiresConsent(false);

        Countly.sharedInstance().init(config);
    }

    @Override
    public void onStop() {
        Countly.sharedInstance().onStop();
        super.onStop();
    }

    @Override
    public void onConfigurationUpdated(Configuration newConfig) {
        super.onConfigurationUpdated(newConfig);
        Countly.sharedInstance().onConfigurationChanged(newConfig);
    }
}
